package com.qianyitian.hope2.analyzer.analyzer;

import com.qianyitian.hope2.analyzer.model.KLineInfo;
import com.qianyitian.hope2.analyzer.model.ResultInfo;
import com.qianyitian.hope2.analyzer.model.Stock;
import com.qianyitian.hope2.analyzer.util.Utils;

import java.util.List;

public class MACDSquareAnalyzer extends AbstractStockAnalyzer {

    private static double N = 3.22;

    public MACDSquareAnalyzer() {
    }

    @Override
    public boolean analyze(ResultInfo resultInfo, Stock stock) {
        //当前MACD > 0
        //前一个MACD <= 0 的周期面积是   M1
        //再之前一个MACD>0 的周期面积是 M2
        //M1 * N < M2
        List<KLineInfo> infos = stock.getkLineInfos();
        if (infos.isEmpty()) {
            return false;
        }

        int currentRedBarNumber = 0;

        KLineInfo today = infos.get(infos.size() - 1);
        double macdToday = today.getMacd();
        if (macdToday < 0) {
            return false;
        }

//        if(getTurnover(stock)< 0.12 *100){
//            //换手>12%
//            return false;
//        }
        double yesterdayMACD = infos.get(infos.size() - 2).getMacd();
        if (macdToday < yesterdayMACD) {
            return false;
        }

        double lastestPositiveMacdSqure = 0;
        double lastestNegativeMacdSqure = 0;

        int i = infos.size() - 1;
        for (; i >= 0; i--) {
            double macd = infos.get(i).getMacd();
            if (macd <= 0) {
                break;
            } else {
                currentRedBarNumber++;
            }
        }

        for (; i >= 0; i--) {
            double macd = infos.get(i).getMacd();
            if (macd <= 0) {
                lastestNegativeMacdSqure = lastestNegativeMacdSqure + macd;
            } else {
                break;
            }
        }
        for (; i >= 0; i--) {
            double macd = infos.get(i).getMacd();
            if (macd > 0) {
                lastestPositiveMacdSqure = lastestPositiveMacdSqure + macd;
            } else {
                break;
            }
        }
        String range = Utils.calcRangeLabel(infos.get(infos.size() - 2).getClose(), today.getClose());

        double times = lastestNegativeMacdSqure == 0 ? 999999 : (lastestPositiveMacdSqure / lastestNegativeMacdSqure * -1);
        boolean ok = times >= N;
        String value=String.format(valueTemplate,currentRedBarNumber,getTurnover(stock),Utils.get2Double(times),range);
        resultInfo.setValue(value);
        return ok;
    }
    static String valueTemplate = "%d-%.1f%%-%.0f倍(%s)";
    public String format(Stock stock) {
        return "";
    }

    @Override
    public String getDescription() {
        return "当前MACD大于0，最近一次MACD绿影面积大幅度小于之前的MACD红影面积" + N;
    }

    public int getWeight() {
        return 150;
    }

}
